/**
 * Copyright (c) 2006 RiseSoft Co.,Ltd  
 * $Header$
 */
package cn.newtest.bi.collection.groovy;

import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;
import groovy.util.ScriptException;

import java.io.File;
import java.io.IOException;

import net.risesoft.risebi.collection.model.Template;
import net.risesoft.risebi.collection.model.TemplateFile;
import net.risesoft.risebi.collection.service.TemplateManager;
import net.risesoft.risebi.commons.util.ContextUtil;
import net.risesoft.risebi.commons.util.ExceptionUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.groovy.control.CompilationFailedException;
import org.springframework.stereotype.Repository;

/**
 *
 * @author <a href="mailto: chenjian@risesoft.net">陈健</a>
 * @version $Revision$
 * 
 */
@Repository(value="groovyLoader")
public class GroovyLoader {

	private static Log log = LogFactory.getLog(GroovyLoader.class);

//	private Map<String, Class<?>> groovyClasses = new HashMap<String, Class<?>>();

	private Class getGroovyClass(String value,String type) throws CompilationFailedException, IOException {
//		if (groovyClasses.containsKey(scriptName))
//			return (Class) groovyClasses.get(scriptName);
		
		if(type.equals("scriptName")){
			GroovyClassLoader loader = new GroovyClassLoader();
			Class groovyClass = loader.parseClass(new File(value));
			//groovyClasses.put(scriptName, groovyClass);
			return groovyClass;
		}else if(type.equals("string")){
			GroovyClassLoader loader = new GroovyClassLoader();
			Class groovyClass = loader.parseClass(value);
			//groovyClasses.put(scriptName, groovyClass);
			return groovyClass;
		}else{
			return null;
		}
	}

	public GroovyObject getGroovyObject(String scriptName) throws Exception {
		try {
			Class groovyClass = getGroovyClass(scriptName,"scriptName");
			GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();
			return groovyObject;
		} catch (Exception e) {
			log.error(ExceptionUtil.transformException(e));
			throw new ScriptException(e);
		}
	}
	
	public GroovyObject getGroovyObject(String templateGuid,String type) throws Exception {
		TemplateManager templateManager = (TemplateManager) ContextUtil.getAc().getBean("templateManager");
		Template template = (Template) templateManager.getObject(Template.class, templateGuid);
		if(template==null)
			return null;
		TemplateFile templateFile = template.getTemplateFile();
		if(templateFile==null)
			return null;
		
		String groovy = null;
		if(type.equals("audit")){
			groovy = templateFile.getAuditGroovy();
			if(groovy==null||groovy.equals(""))
				return null;
		}else{
			groovy = templateFile.getCalculateGroovy();
			if(groovy==null||groovy.equals(""))
				return null;
		}
		
		try {
			Class groovyClass = getGroovyClass(groovy,"string");
			GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();
			return groovyObject;
		} catch (Exception e) {
			log.error(ExceptionUtil.transformException(e));
			throw new ScriptException(e);
		}
	}

}
